import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/game_const.dart';
import 'package:freedom/entity/restful.dart';
import 'package:freedom/entity/meta/meta_room_entity.dart';
import 'package:freedom/util/api_router.dart';
import 'package:freedom/util/customer_dialog.dart';
import 'package:freedom/util/data_type.dart';
import 'package:freedom/util/http.dart';
import 'package:freedom/widget/common_alter_dialog.dart';
import 'package:freedom/util/insert_home.dart';
import '../croom/wz_mahjong.dart';
import '../croom/chengdu_mahjong.dart';

class CreateHomeDialog extends StatefulWidget {
  final String? title, description;
  final Image? image;
  final Function close;

  const CreateHomeDialog({
    super.key,
    @required this.title,
    @required this.description,
    required this.close,
    this.image,
  });

  @override
  CreateHomeDialogState createState() => CreateHomeDialogState();
}

class CrRoomType {
  String? title;
  bool? choice;
  Widget? crWidget;
  String? type;

  CrRoomType.init({this.title, this.choice, this.crWidget, this.type});
}

class CreateHomeDialogState extends State<CreateHomeDialog> {
  final MetaRoomEntity _metaRoomEntity = MetaRoomEntity();

  List<CrRoomType> _rooms = [];

  Widget? _crRoom;

  int _selectIndex = 0;

  @override
  void initState() {
    super.initState();
    _rooms = [
      CrRoomType.init(
          title: "万州麻将",
          crWidget: WzMahjongCroom(_metaRoomEntity),
          type: RoomType.WZ_MAHJONG),
      CrRoomType.init(
          title: "成都麻将",
          crWidget: ChengduMahjong(_metaRoomEntity),
          type: RoomType.CD_MAHJONG),
      /*CrRoomType.init(
          title: "传说中", crWidget: GoldenFlower(null), roomType: null) */
    ];
    _crRoom = _rooms[_selectIndex].crWidget;
    _metaRoomEntity.type = _rooms[_selectIndex].type;
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
      closeWindow: widget.close,
      backgroundColor: Colors.white,
      child: Row(
        children: [
          Expanded(
              flex: 2,
              child: Container(
                color: Colors.green[500],
                child: ListView.builder(
                  itemCount: _rooms.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                        onTap: () {
                          _crRoom = _rooms[index].crWidget;
                          _selectIndex = index;
                          _metaRoomEntity.roomRule = "";
                          setState(() {});
                        },
                        child: Container(
                          alignment: Alignment.center,
                          height: 50.h,
                          decoration: BoxDecoration(
                              color: index == _selectIndex
                                  ? Colors.grey[300]
                                  : Colors.green[500],
                              border: const Border(
                                  bottom: BorderSide(color: Colors.white))),
                          child: Text(
                            _rooms[index].title!,
                            style: TextStyle(
                                color: index == _selectIndex
                                    ? Colors.green[500]
                                    : Colors.grey[300],
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold),
                          ),
                        ));
                  },
                ),
              )),
          Expanded(
              flex: 9,
              child: Container(
                color: Colors.grey[300],
                padding: EdgeInsets.all(10.h),
                child: Column(
                  children: [
                    Expanded(
                        child: SingleChildScrollView(
                      child: _crRoom,
                    )),
                    Container(
                        height: 30.h,
                        alignment: Alignment.centerRight,
                        child: SizedBox(
                          height: 30.h,
                          width: 60.h,
                          child: MaterialButton(
                              padding: const EdgeInsets.all(0),
                              disabledColor: Colors.blue,
                              onPressed: () {
                                if (_metaRoomEntity.type != null) {
                                  Http.getInstance().request<Restful>(
                                      ApiRouter.createRoom,
                                      type: RequestEnum.POST,
                                      contentType: ContentType.json,
                                      responseType: ResponseType.json,
                                      showLoading: true,
                                      showErrorMsg: true,
                                      loadingMsg: "请稍等...",
                                      data: {
                                        "type": _rooms[_selectIndex].type,
                                        "roomRule": _metaRoomEntity.roomRule
                                      }, success: (data) {
                                    widget.close();
                                    MetaRoomEntity roomEntity =
                                        MetaRoomEntity.fromJson(
                                            asT<Map<String, dynamic>>(
                                                data.body));
                                    InsertHome.addHome(
                                        context,
                                        _rooms[_selectIndex].type!,
                                        roomEntity.code!);
                                  }, error: (data) {});
                                } else {
                                  CustomerDialog.showText("无法创建房间");
                                }
                              },
                              child: Container(
                                height: double.infinity,
                                width: double.infinity,
                                decoration: BoxDecoration(
                                    color: Colors.green[500],
                                    borderRadius: BorderRadius.circular(5.r)),
                                child: Center(
                                  child: Text(
                                    "创建",
                                    strutStyle: StrutStyle(
                                      forceStrutHeight: true,
                                      fontSize: 15.h,
                                      fontStyle: FontStyle.italic,
                                      leading: 0.0,
                                    ),
                                    style: TextStyle(
                                        fontSize: 15.h,
                                        height: 1.0,
                                        color: Colors.white,
                                        fontWeight: FontWeight.w800),
                                  ),
                                ),
                              )),
                        ))
                  ],
                ),
              ))
        ],
      ),
    );
  }
}
